import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/LocationEnum.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:mahjong/entity/card/MahjongCardEntity.dart';
import 'package:mahjong/notify/MahjongUserNotify.dart';
import 'package:mahjong/page/mahjong/ExportCard.dart';
import 'package:mahjong/page/mahjong/MahjongExportArea.dart';
import 'package:mahjong/util/CustomNavigatorObserver.dart';
import 'package:mahjong/util/DictionaryConst.dart';
import 'package:mahjong/page/mahjong/dialog/GameCenterUserInfo.dart';

class MahjongCommon {
  static Widget getUserExportCard(MahjongUserNotify provider,
      {bool resv: false}) {
    List<MahjongCardEntity> userExportCards = provider.userExportCards;
    if (userExportCards == null || userExportCards.length == 0) {
      return Container();
    }
    return MahjongExportArea(
        userExportCards.asMap().keys.map((index) {
          return ExportCard(
            userExportCards[index].cardId,
            resv,
            MahjongSystemConst.exportAreaCardWidth,
            index == userExportCards.length - 1 && provider.pervExportUser,
            highlight: userExportCards[index].highlight,
          );
        }).toList(),
        MahjongSystemConst.exportAreaSize,
        MahjongSystemConst.exportAreaSize,
        18);
  }

  static Widget buildHuLocation(bool showWidget, Location userLocation) {
    if (!showWidget) {
      return SizedBox();
    }
    if (userLocation == null) {
      return Container(
        alignment: Alignment.center,
        width: MahjongSystemConst.userHeaderImageWidth,
        height: 18.h,
        decoration: BoxDecoration(
            color: Colors.orange, borderRadius: BorderRadius.circular(5.h)),
        child: Text(
          "自摸",
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 10.sp, color: Colors.white),
        ),
      );
    }
    IconData iconData;
    switch (userLocation) {
      case Location.BOTTOM:
        iconData = Icons.arrow_downward;
        break;
      case Location.LEFT:
        iconData = Icons.arrow_back;
        break;
      case Location.RIGHT:
        iconData = Icons.arrow_forward;
        break;
      case Location.TOP:
        iconData = Icons.arrow_upward;
        break;
      default:
        iconData = Icons.arrow_upward;
    }

    return Container(
      width: MahjongSystemConst.userHeaderImageWidth,
      alignment: Alignment.center,
      height: 18.h,
      decoration: BoxDecoration(
          color: Colors.orange, borderRadius: BorderRadius.circular(5.h)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(
            iconData,
            color: Colors.white,
            size: 15.sp,
          ),
          Text(
            "胡牌",
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 10.sp, color: Colors.white),
          ),
        ],
      ),
    );
  }

  static Widget getUserHeader(MahjongUserNotify provider,
      {bool showOther = true, bool huTop = false, Key key}) {
    return Column(
      key: key,
      children: [
        !huTop || userLocation[provider.firingUser] == null
            ? SizedBox()
            : Column(
                children: [
                  buildHuLocation(
                      provider.win, userLocation[provider.firingUser].location),
                  SizedBox(
                    height: 5.h,
                  ),
                ],
              ),
        InkWell(
            onTap: () {
              showDialog<Null>(
                  context:
                      CustomNavigatorObserver.getInstance().navigator.context,
                  barrierDismissible: false,
                  builder: (BuildContext context) {
                    return GameCenterUserInfo(userChannel: provider);
                  });
            },
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.grey.withOpacity(0.3),
                  border: Border.all(
                    color: Colors.green[200], //边框颜色
                    width: 1, //宽度
                  ),
                  borderRadius: BorderRadius.circular(5.0)),
              height: MahjongSystemConst.userHeaderImageHeight,
              width: MahjongSystemConst.userHeaderImageWidth,
              child: Column(children: [
                Stack(
                  children: [
                    headerImage(provider.sysUser.portraitUrl),
                    !provider.online
                        ? Container(
                            color: Colors.black.withOpacity(0.5),
                            height: MahjongSystemConst.userHeaderImageWidth,
                            width: MahjongSystemConst.userHeaderImageWidth,
                            alignment: Alignment.center,
                            child: Text(
                              "离线...",
                              style: TextStyle(
                                  fontSize: 8.sp, color: Colors.white),
                            ),
                          )
                        : SizedBox(),
                  ],
                ),
                Expanded(
                    child: Container(
                  alignment: Alignment.center,
                  child: Text(
                    provider.integral.toString(),
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 12.sp,
                        color: Colors.yellow,
                        fontWeight: FontWeight.w800),
                  ),
                ))
              ]),
            )),
        huTop || userLocation[provider.firingUser] == null
            ? SizedBox()
            : Column(
                children: [
                  SizedBox(
                    height: 5.h,
                  ),
                  buildHuLocation(
                      provider.win,
                      provider.firingUser == null
                          ? null
                          : userLocation[provider.firingUser].location),
                ],
              )
      ],
    );
  }

  static Widget headerImage(String portraitUrl, {double height, double width}) {
    return ClipRRect(
        child: Container(
      height: height == null ? MahjongSystemConst.userHeaderImageWidth : height,
      width: width == null ? MahjongSystemConst.userHeaderImageWidth : width,
      child: portraitUrl == null
          ? Image.asset(
              "images/mahjong/user/header/common_header.png",
              fit: BoxFit.cover,
            )
          : CachedNetworkImage(
              width: double.infinity,
              height: double.infinity,
              fit: BoxFit.cover,
              placeholder: (context, url) => CircularProgressIndicator(),
              errorWidget: (context, url, error) => Icon(Icons.error),
              imageUrl: DictionaryConst.filePath + portraitUrl,
            ),
    ));
  }

  static buildUserStateText(bool showText, String text,
      {Color color: Colors.yellow}) {
    return showText
        ? Text(
            text,
            style: TextStyle(
                fontSize: 8.sp, color: color, fontWeight: FontWeight.w800),
          )
        : SizedBox();
  }
}
